template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);
[first,last) into a range with all the elements that compare equal to val removed, and returns an iterator to the new end of that range.operator== to compare the individual elements to val.1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator result = first;
while (first!=last) {
if (!(*first == val)) {
if (result!=first)
*result = *first;
++result;
}
++first;
}
return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator result = first;
while (first!=last) {
if (!(*first == val)) {
if (result!=first)
*result = move(*first);
++result;
}
++first;
}
return result;
}
[first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// remove algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::remove
int main () {
int myints[] = {10,20,30,30,20,10,10,20}; // 10 20 30 30 20 10 10 20
// bounds of range:
int* pbegin = myints; // ^
int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^
pend = std::remove (pbegin, pend, 20); // 10 30 30 10 10 ? ? ?
// ^ ^
std::cout << "range contains:";
for (int* p=pbegin; p!=pend; ++p)
std::cout << ' ' << *p;
std::cout << '\n';
return 0;
}
range contains: 10 30 30 10 10
[first,last) are accessed and potentially modified.